﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;


namespace BillingManagment
{
    public partial class AddFarmer : System.Web.UI.Page
    {
        DbConnect db;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind_grdFarmer();
            }
        }

        private void Bind_grdFarmer()
        {
            db = new DbConnect();
            DataTable dt = new DataTable();
            dt = db.GetDataTable("SP_GetFarmerList", CommandType.Text);
            grdFarmer.DataSource = dt;
            grdFarmer.DataBind();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (txtName.Text == "" || txtAddress.Text == "")
            {
                Page.RegisterStartupScript("alert", "<script>alert('Fill Required Field');</script>");
            }
            else
            {
                db = new DbConnect();
                SqlCommand cmd = new SqlCommand("Insert into tbl_Farmer(Name,Village,Phone_No,Other_Info)values('" + txtName.Text + "', '" + txtAddress.Text + "','" + txtPhone.Text + "', '" + txtOtherInfo.Text + "') ");
                db.ExecuteQuery(cmd.CommandText, CommandType.Text);
                FormClear();
                lblMsg.Text = "Data Insert successfully";
            }
            Bind_grdFarmer();
        }

        private void FormClear()
        {
            txtAddress.Text = txtName.Text = txtOtherInfo.Text = txtPhone.Text = string.Empty;
        }

        protected void lbtn_Active_Click(object sender, EventArgs e)
        {
            LinkButton lbtn = (LinkButton)sender;
            GridViewRow row = (GridViewRow)lbtn.NamingContainer;

            Label lbl_ID = (Label)grdFarmer.Rows[row.RowIndex].FindControl("lbl_ID");
            LinkButton lbtn_Active = (LinkButton)grdFarmer.Rows[row.RowIndex].FindControl("lbtn_Active");

            db = new DbConnect();
            if (lbtn_Active.Text == "Active")
            {
                db.ExecuteQuery("SP_tbl_Farmer_Update_Status", CommandType.StoredProcedure,
                    new SqlParameter("@ID", int.Parse(lbl_ID.Text)),
                    new SqlParameter("@IsActive", "0"));
            }
            else
            {
                db.ExecuteQuery("SP_tbl_Farmer_Update_Status", CommandType.StoredProcedure,
                    new SqlParameter("@ID", int.Parse(lbl_ID.Text)),
                    new SqlParameter("@IsActive", "1"));
            }
            Bind_grdFarmer();
        }

        protected void lbtn_Delete_Click(object sender, EventArgs e)
        {
            LinkButton lbtn = (LinkButton)sender;
            GridViewRow row = (GridViewRow)lbtn.NamingContainer;
            Label lbl_ID = (Label)grdFarmer.Rows[row.RowIndex].FindControl("lbl_ID");

            db = new DbConnect();
            db.ExecuteQuery("SP_tbl_Farmer_Delete_ByID", CommandType.StoredProcedure,
                    new SqlParameter("@ID", int.Parse(lbl_ID.Text)));
            Bind_grdFarmer();
        }

        protected void Cancel(object sender, GridViewCancelEditEventArgs e)
        {
            grdFarmer.EditIndex = -1;
            Bind_grdFarmer();
        }

        protected void Edit(object sender, GridViewEditEventArgs e)
        {
            grdFarmer.EditIndex = e.NewEditIndex;
            Bind_grdFarmer();
        }

        protected void Update(object sender, GridViewUpdateEventArgs e)
        {
            Label lbl_ID = (Label)grdFarmer.Rows[e.RowIndex].FindControl("lbl_ID");
            TextBox txt_grd_Name = (TextBox)grdFarmer.Rows[e.RowIndex].FindControl("txt_grd_Name");
            TextBox txt_grd_Village = (TextBox)grdFarmer.Rows[e.RowIndex].FindControl("txt_grd_Village");
            TextBox txt_grd_Phone_no = (TextBox)grdFarmer.Rows[e.RowIndex].FindControl("txt_grd_Phone_no");
            TextBox txt_Other_Info = (TextBox)grdFarmer.Rows[e.RowIndex].FindControl("txt_Other_Info");

            db = new DbConnect();
            db.ExecuteQuery("SP_tbl_Farmer_Update_All", CommandType.StoredProcedure,
                    new SqlParameter("@ID", int.Parse(lbl_ID.Text)),
                    new SqlParameter("@Name", txt_grd_Name.Text),
                    new SqlParameter("@Village", txt_grd_Village.Text),
                    new SqlParameter("@Phone_no", txt_grd_Phone_no.Text),
                    new SqlParameter("@Other_Info", txt_Other_Info.Text));

            grdFarmer.EditIndex = -1;
            Bind_grdFarmer();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddFarmer.aspx");
        }
    }
}